package com.tepth.vehiclemaintenance.ui;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.base.BaseActivity;
import com.tepth.vehiclemaintenance.bean.User;
import com.tepth.vehiclemaintenance.config.SpKey;
import com.tepth.vehiclemaintenance.mvp.login.LoginActivity;
import com.tepth.vehiclemaintenance.tools.AppUtils;
import com.tepth.vehiclemaintenance.tools.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyu on 2017/3/28.
 */

public class SettingActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.tv_user_code)
    TextView tvCode;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.tv_belong_group)
    TextView tvBelongGroup;
    @BindView(R.id.tv_device)
    TextView tvDevice;
    @BindView(R.id.tv_update)
    TextView tvUpdate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    private static final int REQUEST_CODE_LOCATION = 2;

    @Override
    public void initView() {
        initToolBar();
        tvTitle.setText(R.string.preferences_name);
        User user = (User) SPUtils.getObject(this, SpKey.KEY_USER_INFO);
        if (user != null) {
            tvGroup.setText(user.getPersonName());
            tvCode.setText(user.getPersonCode());
            tvAccount.setText(user.getLoginName());
            tvCompany.setText(user.getOrgName());
            tvFactory.setText(user.getDeptName());
            tvBelongGroup.setText(user.getGroupName());
            tvUpdate.setText(String.valueOf(AppUtils.getVersion(this)));
        }

    }

    private void initToolBar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick(R.id.btn_logout)
    public void onLogout() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("reload", "reload");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @OnClick(R.id.layout_update)
    public void onUpdate() {
        Beta.checkUpgrade(true,true);
    }

    @OnClick(R.id.layout_device)
    public void onDevice() {
        startActivity(new Intent(this, NFCListActivity.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvDevice.setText(SPUtils.getString(this, SpKey.KEY_NFC_BIND_MAC, ""));
    }
}
